﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Client
{
    public partial class Client : Form
    {
        TcpClient client = new TcpClient();
        Thread nghe, nhan;
        string ip = "";
        string ipClient = "";
        NetworkStream myns;
        TcpListener mytcpl;
        Socket mysocket;
        Thread myth;
        BinaryReader bb;
        public Client()
        {
            InitializeComponent();
        }
        public void langnghe()
        {
            nhan = new Thread(new ThreadStart(nhadulieu));
            nhan.Start();
        }
        private void nhadulieu()
        {
            try
            {
                byte[] data = new byte[1024];
                string ndung = "", k = "1";
                while (true)
                {
                    int receiv = client.Client.Receive(data);
                    ndung = Encoding.UTF8.GetString(data, 0, receiv).TrimEnd();
                    string[] s = ndung.Split('$');
                    {
                        if (s[0] == ip)
                        {
                            if (s[1] == k)
                            {
                                lbxChat.Items.Add(s[2]);
                            }
                            else
                            {
                                if (s[2] == "TatMay")
                                {
                                    System.Diagnostics.Process.Start("shutdown", "-s -f -t 0");
                                }
                                if (s[2] == "ReStart")
                                {
                                    System.Diagnostics.Process.Start("restart", "-r -f -t 0");
                                }
                                if (s[2] == "LogOff")
                                {
                                    System.Diagnostics.Process.Start("shutdown", "-l -f -t 0");
                                }

                            }
                        }
                    }
                }
            }
            catch
            {
                client.Close();
                Application.Restart();
            }
        }
        private void Client_Load(object sender, EventArgs e)
        {
            ip = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
            ipClient = "192.168.230.1";
            this.Text = ip;
            try
            {
                client.Connect(ipClient, 8080);
                if (client.Client.Connected)
                {
                    lblTinhTrang.Text = "Da ket noi";
                    StreamWriter gui = new StreamWriter(client.GetStream());
                    gui.WriteLine(ip);
                    gui.Flush();
                    nghe = new Thread(new ThreadStart(langnghe));
                    nghe.Start();
                }
                else
                    lblTinhTrang.Text = "Chua ket noi";
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString(), "Loi ket noi");
                if (client != null) client.Client.Close();
            }
        }
        


        private void btnChat_Click(object sender, EventArgs e)
        {
            StreamWriter gui = new StreamWriter(client.GetStream());
            gui.WriteLine(ip + ": " + txtChat.Text);
            gui.Flush();
            lbxChat.Items.Add("Tôi: " + txtChat.Text);
            txtChat.Text = "";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btRec_Click(object sender, EventArgs e)
        {
            
     
            FileDialog fDg = new OpenFileDialog();
            if (fDg.ShowDialog() == DialogResult.OK)
            {
                ClientHelper.SendFile(fDg.FileName);
            }
      
        }
      
    }
}
